package com.yuanss.backenddemo.common;

import lombok.Data;

/**
 * @author yuanss
 * @version 1.0
 * @description 通用结果返回类
 * @date 2024/6/7 22:19:26
 */
@Data
public class R<T> {
    /**
     * 状态码
     */
    private int code;

    /**
     * 状态信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    /**
     * 总条数
     */
    private long total;

    public R() {
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public R(int code, String msg, T data, long total) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.total = total;
    }

    public static R success(Object data) {
        return new R(HttpStatus.SUCCESS, "操作成功", data);
    }

    public static R success(String msg) {
        return new R(HttpStatus.SUCCESS, msg, null);
    }
    public static R success(String msg, Object data) {
        return new R(HttpStatus.SUCCESS, msg, data);
    }
    public static R success(long total, Object data) {
        return new R(HttpStatus.SUCCESS, null, data, total);
    }
    public static R error(String msg) {
        return new R(HttpStatus.ERROR, msg, null);
    }

    public static R error(String errorCode, String errorMsg) {
        return new R(Integer.parseInt(errorCode), errorMsg, null);
    }
}
